//
//  ImagePickerSingleEffect.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/11/29.
//

import Foundation
import UIKit

class ImagePickerSingleEffect: UIControl {
    var urlStr: String? {
        didSet {
            if urlStr != nil {
                imgPicker.urls = [urlStr!]
            }
        }
    }
    
    var image: UIImage? {
        return imgPicker.images.count > 0 ? imgPicker.images[0] : nil
    }
    
    var isRequired: Bool = false {
        didSet {
            updatePlaceholder()
        }
    }
    
    var placeholder: String = "..." {
        didSet {
            updatePlaceholder()
        }
    }
    
    let keyL = UILabel()
    
    init(withPlaceholder _place: String) {
        super.init(frame: .zero)
        self.placeholder = _place
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func updatePlaceholder() {
        keyL.text = placeholder
    }
    
    let height = (ScreenWidth - 100.0) / 2.0

    func setupUI() {
        backgroundColor = .white
        layer.borderColor = UIColor.hex("#E1E1E1").cgColor
        layer.borderWidth = 0.5
        layer.cornerRadius = 10
        
        keyL.text = placeholder
        keyL.textColor = .hex("#666666")
        keyL.font = UIFont.systemFont(ofSize: 14)
        addSubview(keyL)
        keyL.snp.makeConstraints { make in
            make.top.equalTo(self.snp.top).offset(16)
            make.left.equalTo(self.snp.left).offset(11)
        }
        
        addSubview(imgPicker)
        imgPicker.snp.makeConstraints { make in
            make.top.equalTo(keyL.snp.bottom)
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(height + 30)
        }
    }
    
    lazy var imgPicker: BSImagePickerView = {
        let picker = BSImagePickerView()
        picker.uploadType = .photoAndCamera
        picker.key = ""
        picker.rowCount = 2
        picker.maxCount = 1
        picker.itemHeight = height
        return picker
    }()
}
